<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

require APPPATH . 'core/feed/feed_bank.php';

class docs extends CI_Controller {

    protected $data;
    protected $docs_cate_id;

    function __construct() {
        $this->_initdata();
    }

    //Controller: Trang chủ docs
    function index() {
        //get subcate_id from url
        $str = $this->uri->segment(2);
        $a = explode(".", $str);
        $a = explode("-", $a[0]);
        $docs_subcate_id = 40; //$a[count($a) - 1];
        //get cate_id & init data (this page, header, ...)
        $this->data['page_css'] = "tin-tuc";
        $docs_cate = $this->uri->segment(1);
        $this->data['docs_cate'] = $docs_cate;
        //phân các trường hợp xem Tin XNK | Van ban Phap Quy | ... --> show list tin thích hợp
        $docs_cate_id = ""; //biến xác định cate dùng show tin.
        $docs_parent_id = "";
        $page_paging = "";  //biến xác định URL dùng trong paging.
        $index_param = 3;  //biến xác định index của param dùng cho paging.
        $aCate_id = array();
        $docs_cate_id = NEWS_CATE_VBPQ;
        $this->data['page_title'] = $this->data['page_vanbanphapquy'];
        list($this->data['aDocsCate']) = get_docscate_list($docs_cate_id, $this->Fe_docs_model);
        $this->data['docs_cate'] = $docs_cate;
        //cate
        $this->data['docs_subcate_id'] = $docs_subcate_id;
        $this->data['docs_cate_id'] = $docs_cate_id;

        $docs_cate_id = $docs_subcate_id;
        $page_paging = $this->config->item('base_url') . $docs_cate . "/page";
        $index_param = 3;

        $this->_show_docs_list($docs_parent_id, $docs_cate_id, $docs_cate, $page_paging, $index_param);

        $this->show($this->data);
    }

    //Controller: Tim Kiem
    function search() {
        if (!empty($_POST['isPost']) && ($_POST['isPost'] == 1)) {
            $iCurPage = 1;
            $iCurPage = $_POST['h_cur_page'];
            $iRow_Count = 0;
            $limit = PAGESIZE;
            $start = $iCurPage * PAGESIZE - PAGESIZE;
            if (!($start > 0)) {
                $start = 0;
            }
            $eu = ($start - 0);
            if (!$limit > 0) {
                $limit = 20;
            }
            list($iRow_Count, $this->data['aDocs']) = $this->Fe_docs_model->_search_docs_list($_POST['hidCateId'], $_POST['txt_DOCS_Keyword'], $eu, $limit);
            $this->data['paging'] = $this->_paging_search($iCurPage, ceil($iRow_Count / PAGESIZE), $this->config->item('base_url') . "van-ban-phap-quy/", $iRow_Count);
            $this->data['cur_page'] = $iCurPage;
            $this->data['page_css'] = "tin-tuc";
            $this->data['txt_Docs_Keyword'] = $_POST['txt_DOCS_Keyword'];
            $this->data['cbo_docs_cate'] = $_POST['hidCateId'];
            $this->data['cate_name'] = $_POST['txt_DOCS_Cate'];
            $this->data['page_title'] = $this->data['page_vanbanphapquy'];
            $this->data['pagesize'] = PAGESIZE;
            //get danh muc tin (docs_cate)
            list($this->data['aDocsCate']) = get_docscate_list(NEWS_CATE_VBPQ, $this->Fe_docs_model);
            $this->show($this->data);
        }
    }

    function _show_docs_list($new_parent_id, $docs_cate_id, $docs_cate, $page_paging, $index_param) {
        $iCurPage = 1;
        $iCurPage = $this->uri->segment($index_param);
        if ($iCurPage == "")
            $iCurPage = 1;
        $a = explode(".", $iCurPage); //1.html, 2.html
        $iCurPage = $a[0];
        $iRow_Count = 0;
        $limit = PAGESIZE;
        $start = $iCurPage * PAGESIZE - PAGESIZE;
        if (!($start > 0)) {
            $start = 0;
        }
        $eu = ($start - 0);
        if (!$limit > 0) {
            $limit = 20;
        }
        list($iRow_Count, $this->data['aDocs']) = get_docs_desc_paging($this->Fe_docs_model, $new_parent_id, $docs_cate_id, $eu, $limit);

        $this->load->library('Mobile_Detect');
        $detect = new Mobile_Detect();
        if ($detect->isMobile() || $detect->isTablet() || $detect->isAndroidOS())
            $this->data['paging'] = $this->paging_mobile($iRow_Count, $iCurPage, PAGESIZE);
        else
            $this->data['paging'] = $this->_paging($iCurPage, ceil($iRow_Count / PAGESIZE), $page_paging, $iRow_Count);

        $this->data['cur_page'] = $iCurPage;
        $this->data['previous_id'] = 1;
        $this->data['pagesize'] = PAGESIZE;
    }

    function _show_docs_list_mobile($docs_cate_id) {
        list($iRow_Count, $this->data['aDocs']) = $this->Frontendmodel->get_docs_paging($docs_cate_id, 0, 20);
        $this->data['total_records'] = $iRow_Count;
    }

    function _load_language() {
        //language chỉnh trong file: system/language/vn/home_lang.php
        $this->lang->load('docs', 'vn');
        $this->data['page_tinxnk'] = $this->lang->line('page_tinxnk');
        $this->data['page_kienthucxnk'] = $this->lang->line('page_kienthucxnk');
        $this->data['page_nghiepvugiaonhan'] = $this->lang->line('page_nghiepvugiaonhan');
        $this->data['page_nghiepvukhac'] = $this->lang->line('page_nghiepvukhac');
        $this->data['page_thutuchaiquan'] = $this->lang->line('page_thutuchaiquan');
        $this->data['page_vanbanphapquy'] = $this->lang->line('page_vanbanphapquy');
        $this->data['page_tinnoibat'] = $this->lang->line('page_tinnoibat');
        $this->data['page_nodata'] = $this->lang->line('page_nodata');
        $this->data['page_faq'] = $this->lang->line('page_faq');
        $this->data['page_more'] = $this->lang->line('page_more');
        $this->data['page_css'] = $this->lang->line('page_css');
        $this->data['page_header'] = $this->lang->line('page_header');
        $this->data['keyword'] = "Nhập nội dung cần tìm"; //$this->lang->line('keyword');
        $this->data['page_name'] = $this->lang->line('page_name');
        $this->data['page_footer'] = $this->lang->line('page_footer');
    }

    function _initdata() {

        parent::__construct();
        session_start();
        $this->load->helper('eximcached');
        
        $this->load->model('Frontendmodel');
        $this->load->model('Fe_adv_model');
        $this->load->model('Fe_company_model');
        $this->load->model('Fe_docs_model');
        $this->load->model('Fe_forwarding_model');
        $this->load->model('Fe_knowledge_model');
        $this->load->model('Fe_faq_model');
        
        //load submenu and footer
        list($this->data['aLatestNews']) = get_latest_news($this->Frontendmodel, 7);
        list($this->data['aCate2']) = get_newscate_list(NEWS_CATE_VBPQ, $this->Frontendmodel);
        $this->data['aCate3'] = $this->Fe_knowledge_model->get_all_cate();
        $this->data['aCate4'] = _get_all_cate_customs($this->Frontendmodel);
        $this->data['aCate5'] = $this->Fe_forwarding_model->get_all_cate();
        $this->data['aCate6'] = $this->Fe_faq_model->_get_faq_cate_list();

        //load language for home page
        $this->_load_language();

        $this->data['page_header'] = "header";
        $this->data['page_name'] = "docs";
        $this->data['page_footer'] = "footer";
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');
        $this->data['current_url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        $feed = new feed_bank();
        $this->data['gold'] = get_feed_gold($feed);
        $this->data['exchange_rate'] = get_feed_exchange($feed);

        //get Docs list (cateid=2)
        $docs_cate = "van-ban-phap-quy";
        $docs_cate_id = NEWS_CATE_VBPQ;
        list($this->data['aDocs2']) = get_docscate_list($docs_cate_id, $this->Fe_docs_model);
        $this->data['docs_cate_2'] = $docs_cate;
        
        $this->data['page_description'] = "Danh sách các Văn bản pháp quy trong lĩnh vực xuất nhập khẩu: Văn bản VPCP, Văn bản Chính phủ, Văn bản Bộ tài chính, Văn bản các bộ ngành, Văn bản Tổng cục Hải quan, Văn bản Quốc hội.";
        $this->data['page_keywords'] = "Văn bản pháp quy,Văn bản,Công văn,Quyết định,Thông tư,Nghị định,Chỉ thị,Văn bản VPCP,Văn bản Chính phủ,Văn bản Bộ tài chính,Văn bản các bộ ngành,Văn bản Tổng cục Hải quan,Văn bản Quốc hội";
        $this->data['page_image'] = $this->config->item('img_url') . "style/images/logo.jpg";
    }

    //Phân trang docs
    function _paging($page, $totalpage, $pagelink, $totalrows) {
        if ($page == "")
            $page = 1;

        $this->data['cur_page'] = $page;

        $paging_str = '';
        if ($page > 1)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="location.href=' . "'" . $pagelink . '/' . ($page - 1) . ".html'" . '"><img src="' . $this->config->item('img_url') . 'style/images/img20.gif" border="0"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('img_url') . 'style/images/img20.gif" border="0"></a>';

        // list 10 pagelink
        $startpage = 1;
        if ($page > 5)
            $startpage = $page - 4;
        $endpage = $totalpage;
        if ($totalpage - $page > 5)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= '<a href="' . $pagelink . '/' . $i . '.html" class="paging-item">' . $i . '</a>';

        $paging_str .= '<a href="' . $pagelink . '/' . $i . '.html" class="paging-item-selected">' . $page . '</a>';

        for ($i = $page + 1; $i <= $endpage; $i++)
            $paging_str .= '<a href="' . $pagelink . '/' . $i . '.html" class="paging-item">' . $i . '</a>';

        if ($page < $totalpage)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="location.href=' . "'" . $pagelink . '/' . ($page + 1) . ".html'" . '"><img src="' . $this->config->item('img_url') . 'style/images/img21.gif" border="0"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('img_url') . 'style/images/img21.gif" border="0"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';

        return $paging_str;
    }

    function paging_mobile($count, $curPage, $pageSize) {

        $numberPage = ceil($count / $pageSize);

        $startPage = 1;
        $endPage = $numberPage;
        $prev = $curPage > 1 ? $curPage - 1 : $curPage;
        $next = $curPage < $numberPage ? $curPage + 1 : $numberPage;

        if ($endPage > 5) {
            $startPage = $curPage - 2 < 1 ? 1 : $curPage - 2;
            $endPage = $curPage + 2 > $numberPage ? $numberPage : $curPage + 2;
        }

        $html = '<div class="center"><nav><ul class="pagination">';

        if (isset($_GET["txtSearch"])) {
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/tim-kiem/' . $prev . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">&laquo;</a></li>';
            for ($i = $startPage; $i <= $endPage; $i++) {
                if ($curPage == $i) {
                    $html .= '<li class="active"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">' . $i . '<span class="sr-only">(current)</span></a></li>';
                } else {
                    $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/tim-kiem/' . $i . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">' . $i . '</a></li>';
                }
            }
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/tim-kiem/' . $next . '?txtSearch=' . $_GET["txtSearch"] . '&txtCustomsCate=' . $_GET["txtCustomsCate"] . '">&raquo;</a></li>';
        } else {
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/page/' . $prev . '.html">&laquo;</a></li>';
            for ($i = $startPage; $i <= $endPage; $i++) {
                if ($curPage == $i) {
                    $html .= '<li class="active"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/page/' . $i . '.html">' . $i . '<span class="sr-only">(current)</span></a></li>';
                } else {
                    $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/page/' . $i . '.html">' . $i . '</a></li>';
                }
            }
            $html .= '<li class="disabled"><a href="' . $this->config->item('base_url') . 'van-ban-phap-quy/page/' . $next . '.html">&raquo;</a></li>';
        }

        $html .= '</ul></nav></div>';

        return $html;
    }

    //local func: phân trang dùng cho function "Tìm Kiếm"
    function _paging_search($page, $totalpage, $pagelink, $totalrows) {
        $paging_str = '';
        if ($page > 1)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="submitSearch(\'' . $pagelink . '\',' . ($page - 1) . ');return true;"><img src="' . $this->config->item('base_url') . 'style/images/img20.gif" style="cursor:pointer;"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('base_url') . 'style/images/img20.gif" align="absmiddle"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;';

        // list 10 pagelink
        $startpage = 1;
        if ($page > 5)
            $startpage = $page - 4;
        $endpage = $totalpage;
        if ($totalpage - $page > 5)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= ' <a onclick="submitSearch(\'' . $pagelink . '\',' . $i . ');return true;" class="paging-item" href="javascript:void(0);">' . $i . '</a>';
        $paging_str .= ' <a onclick="submitSearch(\'' . $pagelink . '\',' . $i . ');return true;" class="paging-item-selected" href="javascript:void(0);">' . $i . '</a>';
        for ($i = $page + 1; $i <= $endpage; $i++)
            $paging_str .= ' <a onclick="submitSearch(\'' . $pagelink . '\',' . $i . ');return true;" class="paging-item" href="javascript:void(0);">' . $i . '</a>';

        $paging_str .= '&nbsp;&nbsp;&nbsp;';
        if ($page < $totalpage)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="submitSearch(\'' . $pagelink . '\',' . ($page + 1) . ');return true;"><img src="' . $this->config->item('base_url') . 'style/images/img21.gif" style="cursor:pointer;"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('base_url') . 'style/images/img21.gif" align="absmiddle"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
        return $paging_str;
    }

}

?>